package com.baizhi.action;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class UserAction {
    private Integer pageNum;
    private Integer pageSize;
    private PageInfo<User> pageInfo;
    private List<User> users;
    private String userId;
    private String message;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public PageInfo<User> getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo<User> pageInfo) {
        this.pageInfo = pageInfo;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String selectAllUser(){
        UserService userService=new UserServiceImpl();
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=2;
        }
        PageHelper.startPage(pageNum,pageSize);
        users = userService.selectAllUser();
        pageInfo=new PageInfo<>(users,5);
        return "success";
    }

    public String updateUserStatus() throws UnsupportedEncodingException {
        UserService userService=new UserServiceImpl();
        if(pageNum!=null){
            message= URLEncoder.encode( pageNum+"","utf-8");
        }
        User user = userService.selectUserById(userId);
        if (user!=null){
            if(user.getStatus().equals("正常")){
                user.setStatus("禁用");
                userService.updateUserStatus(user);
            }else {
                user.setStatus("正常");
                userService.updateUserStatus(user);
            }
        }
        return "showUserList";
    }
}
